package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.BankBran;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.HashMap;
import java.util.Map;

/**
 * 提现
 */
public class WithdrawActivity extends BaseActivity implements View.OnClickListener {

    private TextView balance_tv;//余额

    private View has_card, no_card;

    private ImageView bank_img1;
    private TextView bank_name_tv;
    private TextView bank_explain;

    private static final int CODE_BANK = 111;
    private String bank_id;

    private EditText et_money;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdraw);

        initTitle();
        initView();
    }

    private void initView() {
        balance_tv = (TextView) findViewById(R.id.balance_tv);

        /**
         * 判断是否有银行卡选择显示隐藏的布局
         */
        has_card = findViewById(R.id.has_card);//有
        no_card = findViewById(R.id.no_card);//无

        has_card.setOnClickListener(this);
        no_card.setOnClickListener(this);

        has_card.setVisibility(View.GONE);// TODO  暂时设为不可见

        bank_img1 = (ImageView) findViewById(R.id.bank_img);
        bank_name_tv = (TextView) findViewById(R.id.bank_name_tv);
        bank_explain = (TextView) findViewById(R.id.bank_explain);
        et_money = (EditText) findViewById(R.id.et_money);

        setbalance(getIntent().getStringExtra("getwall") + "");
    }

    private TextView title;

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("提现");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.btn_main_right).setOnClickListener(this);
        findViewById(R.id.btn_main_right).setVisibility(View.VISIBLE);
        TextView right_tv = (TextView) findViewById(R.id.tv_right);
        right_tv.setVisibility(View.VISIBLE);
        right_tv.setText("确认");
        right_tv.setTextColor(getResources().getColor(R.color.main_color));
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //确认
            case R.id.btn_main_right:
                String money = et_money.getText().toString();
                if (!"".equals(money)) {
                    upload();
                } else {
                    MyToast("请填写提现金额");
                }
                break;

            //有银行卡，前去修改
            case R.id.has_card:
                intent = new Intent(this, BankActivity.class);
                intent.putExtra("from", "提现");
                startActivityForResult(intent, CODE_BANK);
                break;

            //无银行卡，添加收货地址
            case R.id.no_card:
                intent = new Intent(this, BankActivity.class);
                intent.putExtra("from", "提现");
                startActivityForResult(intent, CODE_BANK);
                break;

            default:
                break;
        }
    }

    private static final String TAG = "WithdrawActivity";

    private void upload() {
        showDialog();
        String money = et_money.getText().toString();
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("cardId", bank_id);
        map.put("money", money);
        XUtil.Post(Url.MONEY_TAKEMONEY, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                closeDialog();
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: money= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        MyToast(info);
                        finish();
                    } else {
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }


    private void setbalance(String money) {
        String s = "可用余额" + money + "元";
        SpannableString member = new SpannableString(s);
        int color = getResources().getColor(R.color.main_color);
        member.setSpan(new ForegroundColorSpan(color), 4, money.length() + 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        balance_tv.setText(member);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case CODE_BANK:
                if (resultCode == RESULT_OK) {
                    BankBran bean = (BankBran) data.getSerializableExtra("bankBean");
                    has_card.setVisibility(View.VISIBLE);
                    no_card.setVisibility(View.GONE);
                    bank_id = bean.getBank_id();
                    String bank_name = bean.getBank_name();
                    String bank_branch = bean.getBank_branch();
                    String bank_img = bean.getBank_img();
                    x.image().bind(bank_img1, bank_img);
                    bank_name_tv.setText(bank_name);
                    bank_explain.setText(bank_branch);
                }
                break;

            default:
                break;
        }
    }
}
